package Formularios;
import javax.swing.JOptionPane;
import Dao.MedicoDao;
import DataBase.DataBase;
import Model.Medicos;
import static DataBase.DataBase.*;

/**
 *
 * @author Thiago
 */
public class Cadastro_Medicos extends javax.swing.JInternalFrame {

    int codigo = 0;
    Medicos medico;

    public Cadastro_Medicos() {
        initComponents();
        DataBase.ConectarDataBase();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtcodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txttelefone = new javax.swing.JTextField();
        txtcrm = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtemail = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtnascimento = new javax.swing.JTextField();
        txtcpf = new javax.swing.JTextField();
        txtnome = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtendereco = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        botaoNovo = new javax.swing.JButton();
        botaoCancelar = new javax.swing.JButton();
        botaoExcluir = new javax.swing.JButton();
        botaoSalvar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtespecialidade = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        cbotipocontrato = new javax.swing.JComboBox();
        cbexclusivo = new javax.swing.JCheckBox();
        botaoAlterar = new javax.swing.JButton();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Cadastro Médicos - SISAM");

        txtcodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcodigoActionPerformed(evt);
            }
        });
        txtcodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtcodigoFocusLost(evt);
            }
        });

        jLabel2.setText("Cód. Id:");

        txttelefone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txttelefoneActionPerformed(evt);
            }
        });

        txtcrm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcrmActionPerformed(evt);
            }
        });

        jLabel7.setText("Telefone:");

        jLabel4.setText("Endereço:");

        jLabel3.setText("Nome:");

        txtnascimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtnascimentoActionPerformed(evt);
            }
        });

        txtcpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcpfActionPerformed(evt);
            }
        });

        jLabel10.setText("D.Nascimento:");

        jLabel5.setText("CPF:");

        jLabel8.setText("Email:");

        jLabel9.setText("Especialidade:");

        botaoNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Formularios/Imagens/novo.jpg"))); // NOI18N
        botaoNovo.setText("Novo");
        botaoNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoNovo.setPreferredSize(new java.awt.Dimension(40, 23));
        botaoNovo.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoNovoActionPerformed(evt);
            }
        });

        botaoCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Formularios/Imagens/cancelar.jpg"))); // NOI18N
        botaoCancelar.setText("Cancelar");
        botaoCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoCancelar.setPreferredSize(new java.awt.Dimension(40, 23));
        botaoCancelar.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        botaoExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Formularios/Imagens/excluir.jpg"))); // NOI18N
        botaoExcluir.setText("Excluir");
        botaoExcluir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoExcluir.setPreferredSize(new java.awt.Dimension(40, 23));
        botaoExcluir.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoExcluir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoExcluirActionPerformed(evt);
            }
        });

        botaoSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Formularios/Imagens/salvar.jpg"))); // NOI18N
        botaoSalvar.setText("Salvar");
        botaoSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoSalvar.setPreferredSize(new java.awt.Dimension(40, 23));
        botaoSalvar.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        jLabel6.setText("CRM:");

        jLabel11.setText("Tipo de Contrato:");

        cbotipocontrato.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Diarista", "Quinzenalista", "Mensalista", " " }));
        cbotipocontrato.setToolTipText("Diarista\nSemanal\nQuinzenal\nMensal\n");

        cbexclusivo.setText("Médico Exclusivo");

        botaoAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Formularios/Imagens/alterar.jpg"))); // NOI18N
        botaoAlterar.setText("Alterar");
        botaoAlterar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoAlterar.setPreferredSize(new java.awt.Dimension(40, 23));
        botaoAlterar.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoAlterar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoAlterarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(botaoNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(botaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(botaoAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(botaoExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(botaoCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel11)
                            .addGap(4, 4, 4)
                            .addComponent(cbotipocontrato, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(155, 155, 155)
                            .addComponent(cbexclusivo))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(47, 47, 47)
                                    .addComponent(jLabel2))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(47, 47, 47)
                                    .addComponent(jLabel3))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(38, 38, 38)
                                    .addComponent(jLabel4))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(64, 64, 64)
                                    .addComponent(jLabel5))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(41, 41, 41)
                                    .addComponent(jLabel7))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(59, 59, 59)
                                    .addComponent(jLabel8))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(19, 19, 19)
                                    .addComponent(jLabel9)))
                            .addGap(4, 4, 4)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtespecialidade, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
                                .addComponent(txtemail, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
                                .addComponent(txtendereco, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
                                .addComponent(txtnome, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
                                .addComponent(txtcodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtcpf, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                                        .addComponent(txttelefone, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel6)
                                            .addGap(27, 27, 27))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel10)
                                            .addGap(18, 18, 18)))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txtnascimento)
                                        .addComponent(txtcrm, javax.swing.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)))))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botaoAlterar, botaoCancelar, botaoExcluir, botaoNovo, botaoSalvar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(txtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(txtnome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(txtendereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel5))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtcpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtcrm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txttelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtnascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel8))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel9))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(txtespecialidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel11))
                    .addComponent(cbotipocontrato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbexclusivo))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(botaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botaoNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(botaoAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(botaoExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(botaoCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botaoAlterar, botaoCancelar, botaoExcluir, botaoNovo, botaoSalvar});

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void txtcodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcodigoActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_txtcodigoActionPerformed

	private void txttelefoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txttelefoneActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_txttelefoneActionPerformed

	private void txtcrmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcrmActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_txtcrmActionPerformed

	private void txtnascimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtnascimentoActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_txtnascimentoActionPerformed

	private void txtcpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcpfActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_txtcpfActionPerformed

	private void botaoNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoNovoActionPerformed
            limpacampos();
            destravacampos();
            txtcodigo.setEditable(true);
            txtcodigo.requestFocus();
            botaoAlterar.setEnabled(false);
            botaoCancelar.setEnabled(true);
            botaoExcluir.setEnabled(false);
            botaoNovo.setEnabled(false);
            botaoSalvar.setEnabled(true);
}//GEN-LAST:event_botaoNovoActionPerformed

	private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
            if  (txtcodigo.getText().isEmpty()){
                    botaoAlterar.setEnabled(false);
                    botaoCancelar.setEnabled(false);
                    botaoExcluir.setEnabled(false);
                    botaoNovo.setEnabled(true);
                    botaoSalvar.setEnabled(false);
                    destravacampos();
                }
            else
                {
                    botaoAlterar.setEnabled(true);
                    botaoCancelar.setEnabled(false);
                    botaoExcluir.setEnabled(true);
                    botaoNovo.setEnabled(true);
                    botaoSalvar.setEnabled(false);
                    travacampos();
                }
}//GEN-LAST:event_botaoCancelarActionPerformed

	private void botaoExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoExcluirActionPerformed
             if (medico == null) {
                JOptionPane.showMessageDialog(this, "Selecione alguem para excluir!");
                return;
            } else {
                boolean result = JOptionPane.showInternalConfirmDialog(
                        this, "Deseja realmente excluir esse cadastro?") == 0;
                       
                if(result == true){
                    medico.setCod_medico(Integer.parseInt(txtcodigo.getText()));
                    MedicoDao.deleteMedico(medico);
                    limpacampos();
                    destravacampos();
                    botaoAlterar.setEnabled(false);
                    botaoCancelar.setEnabled(false);
                    botaoExcluir.setEnabled(false);
                    botaoNovo.setEnabled(true);
                    botaoSalvar.setEnabled(true);
                 }else
                     return;
            }
}//GEN-LAST:event_botaoExcluirActionPerformed

        public void travacampos (){
            txtcodigo.setEditable(false);
            txtnome.setEditable(false);
            txtcpf.setEditable(false);
            txtcrm.setEditable(false);
            txtemail.setEditable(false);
            txtendereco.setEditable(false);
            txtespecialidade.setEditable(false);
            txtnascimento.setEditable(false);
            txttelefone.setEditable(false);
            cbexclusivo.setEnabled(false);
            cbotipocontrato.setEnabled(false);
        }
        public void destravacampos (){
            txtcodigo.setEditable(true);
            txtnome.setEditable(true);
            txtcpf.setEditable(true);
            txtcrm.setEditable(true);
            txtemail.setEditable(true);
            txtendereco.setEditable(true);
            txtespecialidade.setEditable(true);
            txtnascimento.setEditable(true);
            txttelefone.setEditable(true);
            cbexclusivo.setEnabled(true);
            cbotipocontrato.setEnabled(true);
        }


        public void limpacampos (){
                txtcodigo.setText(null);
                txtnome.setText(null);
                txtcpf.setText(null);
                txtcrm.setText(null);
                txtemail.setText(null);
                txtendereco.setText(null);
                txtespecialidade.setText(null);
                txtnascimento.setText(null);
                txttelefone.setText(null);
                cbexclusivo.setSelected(false);
                cbotipocontrato.setSelectedIndex(0);
        }

	private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed
            
            medico = new Medicos();

            medico.setCod_medico(Integer.parseInt(txtcodigo.getText()));
            medico.setNome(txtnome.getText());
            medico.setEndereco(txtendereco.getText());
            medico.setCpf(txtcpf.getText());
            medico.setCrm(txtcrm.getText());
            medico.setTelefone(txttelefone.getText());
            medico.setData_nasc(txtnascimento.getText());
            medico.setEmail(txtemail.getText());
            medico.setEspecialidade(txtespecialidade.getText());
            medico.setTipo_contrato(cbotipocontrato.getSelectedIndex());
            if (cbexclusivo.isSelected()){                
                medico.setExclusivo(1);}
            else
                medico.setExclusivo(0);

            if (txtcodigo.isEditable()){
               if(MedicoDao.inserirMedico(medico)){
                    JOptionPane.showMessageDialog(null,"Medico Cadastrado com Sucesso");
               }else{
                    JOptionPane.showMessageDialog(null, "Nao foi possivel Salvar");
               }

                   limpacampos();
                destravacampos();
                botaoAlterar.setEnabled(false);
                botaoCancelar.setEnabled(false);
                botaoExcluir.setEnabled(false);
                botaoNovo.setEnabled(true);
                botaoSalvar.setEnabled(true);

            }else{
                    MedicoDao.AtualizarMedico(medico);
                    travacampos();
                    botaoAlterar.setEnabled(true);
                    botaoCancelar.setEnabled(true);
                    botaoExcluir.setEnabled(true);
                    botaoNovo.setEnabled(true);
                    botaoSalvar.setEnabled(false);
            }
}//GEN-LAST:event_botaoSalvarActionPerformed

        private void txtcodigoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtcodigoFocusLost
          int codMedico = Integer.parseInt(txtcodigo.getText());
          medico =  MedicoDao.localizarMedico(codMedico);

          if (medico != null) {
                txtnome.setText(medico.getNome());
                txtcpf.setText(medico.getCpf());
                txtendereco.setText(medico.getEndereco());
                txtcrm.setText(medico.getCrm());
                txtnascimento.setText(medico.getData_nasc());
                txttelefone.setText(medico.getTelefone());
                txtemail.setText(medico.getEmail());
                txtespecialidade.setText(medico.getEspecialidade());
                cbotipocontrato.setSelectedIndex(medico.getTipo_contrato());
                if (medico.getExclusivo() == 1)
                   cbexclusivo.setSelected(true);
                else
                   cbexclusivo.setSelected(false);
                this.travacampos();
                botaoAlterar.setEnabled(true);
                botaoCancelar.setEnabled(false);
                botaoExcluir.setEnabled(true);
                botaoNovo.setEnabled(true);
                botaoSalvar.setEnabled(false);
          }
        }//GEN-LAST:event_txtcodigoFocusLost

        private void botaoAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAlterarActionPerformed
            this.destravacampos();
            txtcodigo.setEditable(false);
            botaoAlterar.setEnabled(false);
            botaoCancelar.setEnabled(true);
            botaoExcluir.setEnabled(true);
            botaoNovo.setEnabled(false);
            botaoSalvar.setEnabled(true);
        }//GEN-LAST:event_botaoAlterarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoAlterar;
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoExcluir;
    private javax.swing.JButton botaoNovo;
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JCheckBox cbexclusivo;
    private javax.swing.JComboBox cbotipocontrato;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField txtcodigo;
    private javax.swing.JTextField txtcpf;
    private javax.swing.JTextField txtcrm;
    private javax.swing.JTextField txtemail;
    private javax.swing.JTextField txtendereco;
    private javax.swing.JTextField txtespecialidade;
    private javax.swing.JTextField txtnascimento;
    private javax.swing.JTextField txtnome;
    private javax.swing.JTextField txttelefone;
    // End of variables declaration//GEN-END:variables

}
